<?php
/**
 * golangshop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 上海瑞璞玉数字网络科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.golangshop.com
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace addon\cashier\storeapi\controller;

use addon\cashier\model\Group;
use app\model\system\User as UserModel;
use app\model\system\UserGroup;
use app\storeapi\controller\BaseStoreApi;


/**
 * 用户控制器
 * Class User
 * @package addon\shop\siteapi\controller
 */
class User extends BaseStoreApi
{
    /**
     * 用户列表
     * @return mixed
     */
    public function lists()
    {
        $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
        $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
        $status = isset($this->params[ 'status' ]) ? $this->params[ 'status' ] : '';
        $username = isset($this->params[ 'username' ]) ? $this->params[ 'username' ] : '';

        $condition = [
            [ "ug.site_id", "=", $this->site_id ],
            [ "ug.store_id", "=", $this->store_id ],
            [ "ug.app_module", "=", 'store' ]
        ];
        if (!empty($username)) {
            $condition[] = [ 'u.username', 'like', '%' . $username . '%' ];
        }
        if ($status != "") {
            $condition[ "u.status" ] = [ "status", "=", $status ];
        }
        $join = [
            [ 'user u', 'u.uid = ug.uid', 'inner' ],
            [ 'cashier_auth_group cag', 'cag.group_id = ug.group_id', 'inner' ]
        ];
        $field = 'u.uid,u.username,u.is_admin,u.status,u.create_time,cag.group_id,cag.group_name,u.login_time';
        $user_model = new UserGroup();
        $list = $user_model->getUserPageList($condition, $page, $page_size, "u.is_admin desc,u.create_time desc", $field, 'ug', $join);
        return $this->response($list);

    }

    /**
     * 添加用户
     * @return mixed
     */
    public function addUser()
    {
        $username = isset($this->params[ 'username' ]) ? $this->params[ 'username' ] : '';
        $password = isset($this->params[ 'password' ]) ? $this->params[ 'password' ] : '';
        $group_id = isset($this->params[ 'group_id' ]) ? $this->params[ 'group_id' ] : '';

        $user_model = new UserModel();
        $data = array (
            "username" => $username,
            "password" => $password,
            "group_id" => $group_id,
            "app_module" => 'shop',
            "site_id" => $this->site_id,
            "store" => [
                [ 'store_id' => $this->store_id, 'group_id' => $group_id ]
            ]
        );
        $result = $user_model->addUser($data);
        return $this->response($result);
    }

    /**
     * 用户详情
     */
    public function userInfo()
    {
        $uid = $this->params[ 'uid' ] ?? 0;
        if (!$uid) {
            return $this->response($this->success($this->user_info));
        }

        $condition = [
            [ "ug.site_id", "=", $this->site_id ],
            [ "ug.store_id", "=", $this->store_id ],
            [ "ug.uid", "=", $uid ],
            [ "ug.app_module", "=", 'store' ]
        ];
        $join = [
            [ 'user u', 'u.uid = ug.uid', 'inner' ],
            [ 'cashier_auth_group cag', 'cag.group_id = ug.group_id', 'inner' ]
        ];
        $field = 'u.uid,u.username,u.is_admin,u.status,u.create_time,u.login_time,u.login_ip,cag.group_id,cag.group_name';
        $user_model = new UserGroup();
        $user_info = $user_model->getUserInfo($condition, $field, 'ug', $join);
        return $this->response($user_info);
    }

    /**
     * 删除用户
     */
    public function deleteUser()
    {
        $uid = isset($this->params[ 'uid' ]) ? $this->params[ 'uid' ] : 0;
        if ($uid == $this->user_info[ 'uid' ]) return $this->error('', '自己不能删除自己');
        $user_model = new UserGroup();
        $condition = array (
            [ "uid", "=", $uid ],
            [ "site_id", "=", $this->site_id ],
            [ "store_id", "=", $this->store_id ],
        );
        $result = $user_model->deleteUser($condition);
        return $this->response($result);
    }

    /**
     * 管理组列表
     * @return mixed
     */
    public function group()
    {
        $condition = array (
            [ 'site_id', "=", $this->site_id ],
        );
        $group_model = new Group();
        $list = $group_model->getGroupList($condition, 'group_id,group_name');
        return $this->response($list);
    }

    /**
     * 用户日志
     */
    public function userLog()
    {
        $user_model = new UserModel();

        $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
        $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
        $uid = isset($this->params[ 'uid' ]) ? $this->params[ 'uid' ] : 0;
        $search_keys = isset($this->params[ 'search_keys' ]) ? $this->params[ 'search_keys' ] : '';

        $condition = [];
        $condition[] = [ "site_id", "=", $this->site_id ];
        if (!empty($search_keys)) {
            $condition[] = [ 'action_name', 'like', '%' . $search_keys . '%' ];
        }
        if ($uid > 0) {
            $condition[] = [ 'uid', '=', $uid ];
        }

        $list = $user_model->getUserlogPageList($condition, $page, $page_size, "create_time desc");
        return $this->response($list);
    }

    /**
     * 编辑用户
     * @return mixed
     */
    public function editUser()
    {
        $user_model = new UserModel();

        $group_id = isset($this->params[ 'group_id' ]) ? $this->params[ 'group_id' ] : '';
        $status = isset($this->params[ 'status' ]) ? $this->params[ 'status' ] : '';
        $uid = isset($this->params[ 'uid' ]) ? $this->params[ 'uid' ] : 0;

        $condition = array (
            [ "uid", "=", $uid ],
            [ "site_id", "=", $this->site_id ],
            [ "app_module", "=", $this->app_module ],
        );
        $data = array (
            "group_id" => $group_id,
            "status" => $status,
            "store" => [
                [ 'store_id' => $this->store_id, 'group_id' => $group_id ]
            ]
        );

        $this->addLog("编辑用户:" . $uid);

        $result = $user_model->editUser($data, $condition);
        return $this->response($result);

    }

    /**
     * 修改密码
     * */
    public function modifyPassword()
    {
        $site_id = $this->site_id;
        $user_model = new UserModel();
        $uid = $this->uid;

        $old_pass = isset($this->params[ 'old_pass' ]) ? $this->params[ 'old_pass' ] : '';
        $new_pass = isset($this->params[ 'new_pass' ]) ? $this->params[ 'new_pass' ] : '123456';

        $condition = [
            [ 'uid', '=', $uid ],
            [ 'password', '=', data_md5($old_pass) ],
            [ 'site_id', '=', $site_id ]
        ];

        $res = $user_model->modifyAdminUserPassword($condition, $new_pass);
        return $this->response($res);
    }

    /**
     * 获取门店用户权限
     */
    public function userGroupAuth()
    {
        $data = [
            'is_admin' => $this->user_info[ 'is_admin' ],
            'menu_array' => $this->store_list[ $this->store_id ][ 'menu_array' ] ?? ''
        ];
        return $this->response($this->success($data));
    }

}